﻿using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using DataLayer.Types;
using ViewModel.Finance;

namespace MyWpfApp.Finance
{
    /// <summary>
    /// Interaction logic for FrequencyItemListView.xaml
    /// </summary>
    public partial class FrequencyItemListView : UserControl
    {

        private FrequencyListViewModel viewModel;

        public FrequencyItemListView()
        {
            InitializeComponent();

            viewModel = DataContext as FrequencyListViewModel;
        }

        private void listView_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            ShowItemPropertiesDialog();
        }

        private void ShowItemPropertiesDialog()
        {
            FrequencyItem item = listView.SelectedItem as FrequencyItem;
            ShowItemDialog(item);
        }

        private void ItemPropertiesClicked(object sender, RoutedEventArgs e)
        {
            FrequencyItem item = listView.SelectedItem as FrequencyItem;
            ShowItemDialog(item);
        }

        private void NewItemClicked(object sender, RoutedEventArgs e)
        {
            FrequencyItem item = new FrequencyItem();
            ShowItemDialog(item);
        }


        private void DeleteItem(FrequencyItem item)
        {
            if (MessageBox.Show("Are you sure you want to delete this item?\n\r\n\r" + item.Name) != MessageBoxResult.OK) return;

            viewModel.DeleteItem(item);
        }


        private void ShowItemDialog(FrequencyItem item)
        {
            FrequencyItemPropertiesView propertiesView = new FrequencyItemPropertiesView();
            propertiesView.DataContext = new FrequencyPropertiesViewModel(item);
            propertiesView.ShowDialog();
        }

        private void DeleteItem_Click(object sender, RoutedEventArgs e)
        {
            DeleteItem(listView.SelectedItem as FrequencyItem);
        }
    }
}